package com.elean.springboot.superprint.autoconfigure.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @version 1.0
 * @program: superprint-service
 * @className: SignUtil
 * @description:
 * @author: cxb
 * @create: 2021-03-03 15:20
 **/
@Log4j2
public class SignUtil {
    private final static String excludes = "serialVersionUID,result,signStr";
    public static String getSignStr(Object object, String key) {
        // 1、获取本类字段
        // 2、获取父类字段
        return getSignStr(getAllFileds(object), key);
    }
    private static List<NameValueObj> getAllFileds(Object object) {
        List<NameValueObj> list = new ArrayList<>();
        String jsonString = JSONObject.toJSONString(object);
        JSONObject jobject = JSONObject.parseObject(jsonString);
        for (Map.Entry<String, Object> nameValueObj : jobject.entrySet()) {
            if (nameValueObj.getValue() != null&&isBaseDataType(nameValueObj.getValue().getClass())) {
                if(nameValueObj.getValue().getClass().equals(String.class)){
                    if(!StringUtils.isEmpty((CharSequence) ((String) nameValueObj.getValue()).trim())){//当为字符串时不能为空字符串
                        NameValueObj val = new NameValueObj(nameValueObj.getKey(), nameValueObj.getValue().toString());
                        list.add(val);
                    }
                }else{
                    NameValueObj val = new NameValueObj(nameValueObj.getKey(), nameValueObj.getValue().toString());
                    list.add(val);
                }

            }
        }
        return list;
    }
    /**
     * 计算签名
     * @param key key
     * @param list 内容
     * @return 签名结果
     */
    public static String getSignStr(List<NameValueObj> list, String key) {
        log.info("［生产signstr］待生成的串{} ", JSONObject.toJSONString(list));
        System.out.println("［生产signstr］待生成的串{} "+ JSONObject.toJSONString(list));
        List<String> strings = new ArrayList<String>();
        for (NameValueObj nameValueObj : list) {
            if (nameValueObj.getValue()!=null) {
                strings.add(nameValueObj.getName() + "=" + nameValueObj.getValue() + "&");
            }
        }
        strings.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.toLowerCase().compareTo(o2.toLowerCase());
            }
        });
        StringBuilder sb = new StringBuilder();
        strings.forEach(new java.util.function.Consumer<String>() {
            @Override
            public void accept(String t) {
                sb.append(t);
            }
        });
        sb.append("key=").append(key);
        log.info("［生产signstr］待加密的串 {}", sb.toString());
        String str = DigestUtils.md5DigestAsHex(sb.toString().getBytes());
        log.info("［生产signstr］加密后的串 {}", str);
        return str;
    }
    public static boolean isBaseDataType(Class<?> clazz) {
        return (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class) || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.equals(LocalDateTime.class)  || clazz.isPrimitive());
    }
}
